package org.dromara.nav.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.nav.domain.bo.OsProjectBo;
import org.dromara.nav.domain.vo.OsProjectVo;
import org.dromara.nav.domain.OsProject;
import org.dromara.nav.mapper.OsProjectMapper;
import org.dromara.nav.service.IOsProjectService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 开源项目Service业务层处理
 *
 * @author LSY
 * @date 2025-11-13
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class OsProjectServiceImpl implements IOsProjectService {

    private final OsProjectMapper baseMapper;

    /**
     * 查询开源项目
     *
     * @param projectId 主键
     * @return 开源项目
     */
    @Override
    public OsProjectVo queryById(Long projectId){
        return baseMapper.selectVoById(projectId);
    }

    /**
     * 分页查询开源项目列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 开源项目分页列表
     */
    @Override
    public TableDataInfo<OsProjectVo> queryPageList(OsProjectBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OsProject> lqw = buildQueryWrapper(bo);
        Page<OsProjectVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的开源项目列表
     *
     * @param bo 查询条件
     * @return 开源项目列表
     */
    @Override
    public List<OsProjectVo> queryList(OsProjectBo bo) {
        LambdaQueryWrapper<OsProject> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OsProject> buildQueryWrapper(OsProjectBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OsProject> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(OsProject::getProjectId);
        lqw.like(StringUtils.isNotBlank(bo.getProjectName()), OsProject::getProjectName, bo.getProjectName());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectCode()), OsProject::getProjectCode, bo.getProjectCode());
        lqw.eq(StringUtils.isNotBlank(bo.getLogo()), OsProject::getLogo, bo.getLogo());
        lqw.eq(StringUtils.isNotBlank(bo.getWebsite()), OsProject::getWebsite, bo.getWebsite());
        lqw.eq(StringUtils.isNotBlank(bo.getSourceUrl()), OsProject::getSourceUrl, bo.getSourceUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getDemoUrl()), OsProject::getDemoUrl, bo.getDemoUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), OsProject::getDescription, bo.getDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getLicense()), OsProject::getLicense, bo.getLicense());
        lqw.eq(StringUtils.isNotBlank(bo.getLang()), OsProject::getLang, bo.getLang());
        lqw.eq(bo.getLastCommit() != null, OsProject::getLastCommit, bo.getLastCommit());
        lqw.eq(bo.getStarCount() != null, OsProject::getStarCount, bo.getStarCount());
        lqw.eq(bo.getForkCount() != null, OsProject::getForkCount, bo.getForkCount());
        lqw.eq(bo.getDownloadCount() != null, OsProject::getDownloadCount, bo.getDownloadCount());
        lqw.eq(bo.getSort() != null, OsProject::getSort, bo.getSort());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), OsProject::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增开源项目
     *
     * @param bo 开源项目
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(OsProjectBo bo) {
        OsProject add = MapstructUtils.convert(bo, OsProject.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setProjectId(add.getProjectId());
        }
        return flag;
    }

    /**
     * 修改开源项目
     *
     * @param bo 开源项目
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(OsProjectBo bo) {
        OsProject update = MapstructUtils.convert(bo, OsProject.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(OsProject entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除开源项目信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
